/////////////////////////////////////////////////////////////////////////////
// Name:        DownloadPkgDialog.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     14/01/2009 14:22:08
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "DownloadPkgDialog.h"
#include "config.h"
#include "common.h"
#include "events.h"
#include "pkgmgr.h"

////@begin XPM images
////@end XPM images


/*!
 * DownloadPkgDialog type definition
 */

IMPLEMENT_DYNAMIC_CLASS( DownloadPkgDialog, wxDialog )


/*!
 * DownloadPkgDialog event table definition
 */

BEGIN_EVENT_TABLE( DownloadPkgDialog, wxDialog )

////@begin DownloadPkgDialog event table entries
    EVT_BUTTON( wxID_CANCEL, DownloadPkgDialog::OnCANCELClick )

////@end DownloadPkgDialog event table entries
	EVT_COMMAND( ID_DOWNLOAD_PKG_DIALOG, wxEVT_PKG_DOWNLOAD_BEGIN_EVENT, DownloadPkgDialog::OnPkgDownloadBegin)
	EVT_COMMAND( ID_DOWNLOAD_PKG_DIALOG, wxEVT_PKG_DOWNLOADING_EVENT, DownloadPkgDialog::OnPkgDownloading)
	EVT_COMMAND( ID_DOWNLOAD_PKG_DIALOG, wxEVT_PKG_DOWNLOAD_COMPLETED_EVENT, DownloadPkgDialog::OnPkgDownloadCompleted)
	EVT_COMMAND( ID_DOWNLOAD_PKG_DIALOG, wxEVT_PKGS_DOWNLOAD_BEGIN_EVENT, DownloadPkgDialog::OnPkgsDownloadBegin)
	EVT_COMMAND( ID_DOWNLOAD_PKG_DIALOG, wxEVT_PKGS_DOWNLOAD_COMPLETED_EVENT, DownloadPkgDialog::OnPkgsDownloadCompleted)
END_EVENT_TABLE()


/*!
 * DownloadPkgDialog constructors
 */

DownloadPkgDialog::DownloadPkgDialog()
{
    Init();
}

DownloadPkgDialog::DownloadPkgDialog( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, caption, pos, size, style);
}


/*!
 * DownloadPkgDialog creator
 */

bool DownloadPkgDialog::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin DownloadPkgDialog creation
    SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, size, style );

    CreateControls();
    Centre();
////@end DownloadPkgDialog creation
    return true;
}


/*!
 * DownloadPkgDialog destructor
 */

DownloadPkgDialog::~DownloadPkgDialog()
{
////@begin DownloadPkgDialog destruction
////@end DownloadPkgDialog destruction
    EventCenter::get().unregister(wxEVT_PKG_DOWNLOAD_BEGIN_EVENT, this);
    EventCenter::get().unregister(wxEVT_PKG_DOWNLOADING_EVENT, this);
    EventCenter::get().unregister(wxEVT_PKG_DOWNLOAD_COMPLETED_EVENT, this);
    EventCenter::get().unregister(wxEVT_PKGS_DOWNLOAD_BEGIN_EVENT, this);
    EventCenter::get().unregister(wxEVT_PKGS_DOWNLOAD_COMPLETED_EVENT, this);
}


/*!
 * Member initialisation
 */

void DownloadPkgDialog::Init()
{
////@begin DownloadPkgDialog member initialisation
    p_download_info_static = NULL;
    p_total_size_static = NULL;
    p_file_name_static = NULL;
    p_file_size_static = NULL;
    p_speed_static = NULL;
    p_downloaded_static = NULL;
    p_download_gauge = NULL;
    p_ok_button = NULL;
////@end DownloadPkgDialog member initialisation
}


/*!
 * Control creation for DownloadPkgDialog
 */

void DownloadPkgDialog::CreateControls()
{    
////@begin DownloadPkgDialog content construction
    DownloadPkgDialog* itemDialog1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemDialog1->SetSizer(itemBoxSizer2);

    p_download_info_static = new wxStaticText( itemDialog1, wxID_STATIC, _("Downloading packages from : "), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer2->Add(p_download_info_static, 0, wxGROW|wxALL, 5);

    wxGridSizer* itemGridSizer4 = new wxGridSizer(2, 2, 0, 0);
    itemBoxSizer2->Add(itemGridSizer4, 1, wxALIGN_LEFT|wxLEFT|wxRIGHT, 5);

    wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("Total size : "), wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(itemStaticText5, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_total_size_static = new wxStaticText( itemDialog1, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(p_total_size_static, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText7 = new wxStaticText( itemDialog1, wxID_STATIC, _("Current file name : "), wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(itemStaticText7, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_file_name_static = new wxStaticText( itemDialog1, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(p_file_name_static, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText9 = new wxStaticText( itemDialog1, wxID_STATIC, _("Current file size : "), wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(itemStaticText9, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_file_size_static = new wxStaticText( itemDialog1, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(p_file_size_static, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText11 = new wxStaticText( itemDialog1, wxID_STATIC, _("Download speed : "), wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(itemStaticText11, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_speed_static = new wxStaticText( itemDialog1, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(p_speed_static, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText13 = new wxStaticText( itemDialog1, wxID_STATIC, _("Downloaded : "), wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(itemStaticText13, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_downloaded_static = new wxStaticText( itemDialog1, wxID_STATIC, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    itemGridSizer4->Add(p_downloaded_static, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticLine* itemStaticLine15 = new wxStaticLine( itemDialog1, wxID_STATIC, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    itemBoxSizer2->Add(itemStaticLine15, 0, wxGROW|wxALL, 5);

    p_download_gauge = new wxGauge( itemDialog1, ID_GAUGE, 100, wxDefaultPosition, wxSize(250, -1), wxGA_HORIZONTAL );
    p_download_gauge->SetValue(0);
    itemBoxSizer2->Add(p_download_gauge, 0, wxGROW|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer17 = new wxStdDialogButtonSizer;

    itemBoxSizer2->Add(itemStdDialogButtonSizer17, 0, wxALIGN_RIGHT|wxALL, 5);
    wxButton* itemButton18 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer17->AddButton(itemButton18);

    p_ok_button = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
    p_ok_button->Enable(false);
    itemStdDialogButtonSizer17->AddButton(p_ok_button);

    itemStdDialogButtonSizer17->Realize();

////@end DownloadPkgDialog content construction
    
	_cur_size = 0L;
	_total_size = 0L;
	_total_downloaded = 0L;
	_downloaded = 0;
	_speed = 0;
	
    _watch.Pause();

    EventCenter::get().registerr(wxEVT_PKG_DOWNLOAD_BEGIN_EVENT, this);
    EventCenter::get().registerr(wxEVT_PKG_DOWNLOADING_EVENT, this);
    EventCenter::get().registerr(wxEVT_PKG_DOWNLOAD_COMPLETED_EVENT, this);
    EventCenter::get().registerr(wxEVT_PKGS_DOWNLOAD_BEGIN_EVENT, this);
    EventCenter::get().registerr(wxEVT_PKGS_DOWNLOAD_COMPLETED_EVENT, this);
    
	p_download_info_static->SetLabel(wxString::Format(_("Downloading packages from : %s"), 
				Config::get().get_last_mirror().c_str()) );
}


/*!
 * Should we show tooltips?
 */

bool DownloadPkgDialog::ShowToolTips()
{
    return true;
}

/*!
 * Get bitmap resources
 */

wxBitmap DownloadPkgDialog::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin DownloadPkgDialog bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end DownloadPkgDialog bitmap retrieval
}

/*!
 * Get icon resources
 */

wxIcon DownloadPkgDialog::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin DownloadPkgDialog icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end DownloadPkgDialog icon retrieval
}


void DownloadPkgDialog::OnPkgDownloadBegin(wxCommandEvent &event) {
	_cur_size = event.GetInt();
	_cur_file_name = event.GetString();
	_downloaded = 0;
	
	p_file_name_static->SetLabel(_cur_file_name);
	p_file_name_static->Refresh();
	p_file_size_static->SetLabel(format_size(_cur_size));
	p_file_size_static->Refresh();

	_watch.Start();
	
	_refresh_status();
}
void DownloadPkgDialog::OnPkgDownloading(wxCommandEvent &event) {
	_downloaded = event.GetInt();
	
	// get and show download speed
	_speed = _downloaded/(_watch.Time()/1000.0);
	_refresh_status();
}
void DownloadPkgDialog::OnPkgDownloadCompleted(wxCommandEvent &event) {
	_total_downloaded = event.GetInt();
	_downloaded = 0;
	_refresh_status();
	_watch.Pause();
}
void DownloadPkgDialog::OnPkgsDownloadBegin(wxCommandEvent &event) {
	_total_size = event.GetInt();
	_cur_size = 0;
	_total_downloaded = 0;
	_downloaded = 0;
	_speed = 0;
	
	p_total_size_static->SetLabel(format_size(_total_size));
	p_total_size_static->Refresh();
}
void DownloadPkgDialog::OnPkgsDownloadCompleted(wxCommandEvent &event) {
	_downloaded = 0;
	_watch.Pause();
	_refresh_status();
	
	// get error code & error string
	int err_code = event.GetInt();
	const wxString& err_str = event.GetString();
	
	if (0!=err_code) {
		wxMessageBox(err_str, _("Error!"),
				wxICON_ERROR | wxCANCEL, this);
		this->EndModal(wxID_CANCEL);
		return;
	}
	
	this->EndModal(wxID_OK);
}

bool DownloadPkgDialog::_refresh_status() {
	int i = 0;
	
	if (_total_size>0) {
		i = (double(_total_downloaded+_downloaded)/double(_total_size))*100;
	}
	
	p_speed_static->SetLabel(format_size(_speed).Append(_T("/s")));
	p_downloaded_static->SetLabel(format_size(_total_downloaded+_downloaded));
	
	p_speed_static->Refresh();
	p_downloaded_static->Refresh();
	
	// wxMessageBox(wxString::Format(_T("%d"),_downloaded));
	p_download_gauge->SetValue(i);
}


/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_CANCEL
 */

void DownloadPkgDialog::OnCANCELClick( wxCommandEvent& event )
{
    event.Skip();
    PkgMgr::get().stop_tasks();
}

